package vn.edu.saigontech.ncms.service.impl;

import java.util.Date;

import vn.edu.saigontech.ncms.model.info.MultiTimeReceipt;
import vn.edu.saigontech.ncms.model.system.Money;
import vn.edu.saigontech.ncms.service.MultiTimeReceiptService;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.StudentService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;

public class MultiTimeReceiptServiceImpl extends HibernateGenericService<MultiTimeReceipt, Long> implements MultiTimeReceiptService {
	
	@Override
	public MultiTimeReceipt add(String number, Long studentId, Long offeringId,
			Date createdDate, Money fee, Long cashierId, String comment,
			int submittedTime, Money submittedFee, Date expiredDate) {
		// TODO Auto-generated method stub
		MultiTimeReceipt offeringReceipt = new MultiTimeReceipt();
		offeringReceipt.setNumber(number);
		offeringReceipt.setStudent(StudentService.INSTANCE.findById(studentId));
		offeringReceipt.setOffering(OfferingService.INSTANCE.findById(offeringId));
		offeringReceipt.setDate(createdDate);
		offeringReceipt.setFee(fee);
		offeringReceipt.setActive(true);
		offeringReceipt.setCashier(UserService.INSTANCE.findById(cashierId));
		offeringReceipt.setComment(comment);
		
		offeringReceipt.setSubmittedTime(submittedTime);
		offeringReceipt.setTotalFee(submittedFee);
		offeringReceipt.setExpiredDate(expiredDate);
		
		if(!Checker.isNull(super.persist(offeringReceipt))) {
			OfferingMemberService.INSTANCE.add(offeringReceipt.getId());
			return offeringReceipt;
		}
		return null;
	}

	@Override
	public MultiTimeReceipt edit(Long receiptId, String receiptNo,
			Long offeringId, Date createdDate, Money fee, Long cashierId,
			boolean active, String comment, int submittedTime,
			Money submittedFee, Date expiredDate) {
		MultiTimeReceipt receipt = (MultiTimeReceipt) this.findById(receiptId);
		receipt.setOffering(OfferingService.INSTANCE.findById(offeringId));
		receipt.setNumber(receiptNo);
		receipt.setDate(createdDate);
		receipt.setFee(fee);
		receipt.setCashier(UserService.INSTANCE.findById(cashierId));
		receipt.setActive(active);
		receipt.getOfferingMember().setActive(active);
		receipt.setComment(comment);
		
		receipt.setSubmittedTime(submittedTime);
		receipt.setTotalFee(submittedFee);
		receipt.setExpiredDate(expiredDate);
		
		return (MultiTimeReceipt) super.persist(receipt);
	}
}